import {request} from "../../utils/request"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    songList:[],
    swiperList:[],
    activeList:[],
    active:{} as any,
    show:false
  },
  toSong(){
    wx.navigateTo({
      url:"/pages/position/position"
    })
  },
  toDaiLiu(){
    wx.navigateTo({
      url:"/pages/dailiu/dailiu"
    })
  },
  toWeiYang(){
    wx.navigateTo({
      url:"/pages/weiyang/weiyang"
    })
  },
  toSearch(){
    wx.navigateTo({
      url:"/pages/xunchong/xunchong"
    })
  },
  toContext(e:any){
    let index = e.currentTarget.dataset.index
    let currentData = this.data.songList[index]
    wx.navigateTo({
      url:"/pages/lyContext/lyContext",
      success(res){
        res.eventChannel.emit('context',currentData)
      }
    })
  },
  showMore(){  
    wx.switchTab({
      url:"/pages/adopt/adopt"
    })
  },
  showActive(e:any){
    let index = e.currentTarget.dataset.index
    let active = this.data.activeList[index]    
    this.setData({
      show:true,
      active
    })
  },
  activeClose(){
    this.setData({
      show:false
    })
  },
  async joinActive(){
    const { id } = wx.getStorageSync("userInfo")
    const activeId = this.data.active.id
    if(id){
        let result = await request('/active/join',"POST",{userId:id,activeId}) as any
        if(result.data.code==400){
          wx.showToast({title:"不能重复参加",icon:"none"})
          this.setData({
            show:false
          })
        }else{
          wx.showToast({title:"参加成功",icon:"success"})
        }
    }else{
      wx.showToast({title:"请先登录",icon:"none"})
    }
  },
  async onShow() {
    let songData = await request("/song") as any
    let swiperData = await request("/swiper") as any
    let activeData = await request("/active") as any
    let songListData = songData.data.map((item:any)=>{
      item.createTime = item.createTime.substring(0,10)
      return item
    })
    let songList = songListData.filter((item:any,index:number)=>{
      return index <= 5
    })
    this.setData({
      songList,
      swiperList:swiperData.data,
      activeList:activeData.data
    })
    
  }
})